/* 
 * Copyright (c) 2004-2005 Massachusetts Institute of Technology. This code was
 * developed as part of the Haystack (http://haystack.lcs.mit.edu/) research 
 * project at MIT. Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation files (the 
 * "Software"), to deal in the Software without restriction, including without 
 * limitation the rights to use, copy, modify, merge, publish, distribute, 
 * sublicense, and/or sell copies of the Software, and to permit persons to whom
 * the Software is furnished to do so, subject to the following conditions: 
 * 
 * The above copyright notice and this permission notice shall be included in 
 * all copies or substantial portions of the Software. 
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER  
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE. 
 */
/*
 * Created on Jun 13, 2004
 *
 */
package edu.mit.csail.relo.jdt.parts;


import org.eclipse.draw2d.FigureListener;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Insets;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.CompoundCommand;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IMenuManager;

import edu.mit.csail.relo.eclipse.gef.IncrementalGraphicalEditPart;
import edu.mit.csail.relo.figures.PackageFigure;
import edu.mit.csail.relo.jdt.CodeUnit;
import edu.mit.csail.relo.jdt.RJCore;
import edu.mit.csail.relo.parts.ReloController;

/**
 * @author vineet
 *
 */
public class PackageEditPart extends CodeUnitEditPart {

	/* (non-Javadoc)
	 * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#createFigure()
	 */
    @Override
    protected IFigure createFigure(IFigure curFig, int newDL) {
		if (curFig == null) {
		    final PackageFigure pckgFig = new PackageFigure(
		            getLabel(), 
		            CodeUnit.getIcon(getRepo(), getCU(), RJCore.packageType));
            final IFigure moreBtnFig = getMoreBtn();
			pckgFig.getContentPane().add(moreBtnFig);
			pckgFig.addFigureListener(new FigureListener() {
	            public void figureMoved(IFigure source) {
	        	    Rectangle pkgBounds = pckgFig.getBounds();
	        	    
	        	    Dimension figSize = moreBtnFig.getPreferredSize().getCopy();
	        	    moreBtnFig.setSize(figSize);
	        	    moreBtnFig.setLocation(pkgBounds.getBottomRight().translate(figSize.negate()).translate(-2, -2));
	            }});
			curFig = pckgFig;
		}
		return curFig;
	}

	
    @Override
    protected Insets contributeSubgraghInsets() {
        // make sure there is space for the more button
        Insets sgInsets = super.contributeSubgraghInsets();
        Dimension figSize = getMoreBtn().getPreferredSize();
        sgInsets.right = sgInsets.right + figSize.width + sgInsets.right;
        sgInsets.bottom = figSize.height + sgInsets.bottom;
        return sgInsets;
    }

    @Override
    protected String getChildrenLabel() {
        return "Classes";
    }

    @Override
    public void buildContextMenu(IMenuManager menu) {
    	// we don't want the CUEP 'Open in Editor' context menu
        //super.buildContextMenu(menu);
        
	    final PackageEditPart packageCUEP = this;

	    IAction action;
        action = new Action("Hide Package") {
            @Override
            public void run() {
                CompoundCommand actionCmd = new CompoundCommand();
                actionCmd.add(new Command("hide package") {
                    ReloController rc = null;
					IncrementalGraphicalEditPart parentIGEP = null;
                    @Override
                    public void execute() { 
                        if (rc == null) rc = getRootController();
                        parentIGEP = packageCUEP.hideAEP();
                    }
                    @Override
                    public void undo() {
                    	rc.addNode(packageCUEP, parentIGEP);
                        packageCUEP.assertParenthood();
                    }
                });
                //cuep.realizeParent(actionCmd, /*inferring*/ false);
                PackageEditPart.this.execute(actionCmd);
            }
        };
        menu.appendToGroup("main", action);
    }

}
